import { create } from 'zustand';
import { devtools } from 'zustand/middleware';

export interface AdminInfoState {
  adminInfo: {
    id: string; // 用户id
    username: string; // 用户名
    phone: string; // 手机号
    password: string; // 密码
    avatar: string; // 头像
    createdAt: string; // 创建时间
  };
  token: string; // 令牌
}


interface AdminInfoActions {
  setAdminInfo: (adminInfo: any) => void;
  setToken: (token: string) => void;
}

// 管理员信息
const useAdminInfoStore = create(devtools<AdminInfoState & AdminInfoActions>((set) => ({
  adminInfo: localStorage.getItem('adminInfo') ? JSON.parse(localStorage.getItem('adminInfo') || '{}') : {} as any,
  token: localStorage.getItem('AdminToken') || '',
  setAdminInfo: (adminInfo: any) => {
    set({ adminInfo });
    if (Object.keys(adminInfo).length > 0) {
      localStorage.setItem('adminInfo', JSON.stringify(adminInfo));
    } else {
      localStorage.removeItem('adminInfo');
    }
  },
  setToken: (token: string) => {
    set({ token });
    if (token.length > 0) {
      localStorage.setItem('AdminToken', token);
    } else {
      localStorage.removeItem('AdminToken');
    }
  },
})));

export default useAdminInfoStore;
